#ifndef BCD_H_
#define BCD_H_

#include <time.h>

typedef unsigned char byte;

#ifdef  __cplusplus
extern "C" {
#endif

uint8_t get_bcd(byte b);

void set_bcd(byte *b, uint8_t value);

const char *sprintf_bcd(char *buf, const byte *b, uint8_t digits);

void sscanf_bcd(const char *buf, byte *b, uint8_t digits);

int32_t read_int(const byte *b, uint8_t digits);

void write_int(byte *b, uint8_t digits, int32_t value);

uint32_t read_unsigned_int(const byte *b, uint8_t digits);

void write_unsigned_int(byte *b, uint8_t digits, uint32_t value);

uint64_t read_unsigned_int64(const byte *b, uint8_t digits);

void write_unsigned_int64(byte *b, uint8_t digits, uint64_t value);

double read_decimal(const byte *b, uint8_t digits, uint8_t decimals);

double read_unsigned_decimal(const byte *b, uint8_t digits, uint8_t decimals);

typedef enum _date_offset_e {
    DT_OFFSET_SECOND = 0,
    DT_OFFSET_MINUTE = DT_OFFSET_SECOND + 2,
    DT_OFFSET_HOUR = DT_OFFSET_MINUTE + 2,
    DT_OFFSET_DAY = DT_OFFSET_HOUR + 2,
    DT_OFFSET_MONTH = DT_OFFSET_DAY + 2,
    DT_OFFSET_YEAR = DT_OFFSET_MONTH + 2
} date_offset_e;

time_t read_datetime(const byte *b, uint8_t digits, date_offset_e offset);

#ifdef  __cplusplus
}
#endif

#endif /* BCD_H_ */
